#ifndef BEELDVERWERKING_H
#define BEELDVERWERKING_H

#include <iostream>
#include "cv.h"
#include "highgui.h"
#include <math.h>

using namespace cv;
using namespace std;

enum ASSIGNMENT_TYPE {
	ASSIGNMENT_SETTING = 0,
	ASSIGNMENT_CREATE_SPEELVELD,
	ASSIGNMENT_MONITOR_ORIENTATION,
	ASSIGNMENT_SHUTDOWN,
	ASSIGNMENT_CAM_THRESHOLD,
	ASSIGNMENT_CAM_CONNECT_DISCONNECT
};

typedef struct assignment {
	enum ASSIGNMENT_TYPE type;

	// Setting data
	unsigned char camSelect;
	unsigned char thresHold;
	bool camConnect;

	// Create speelveld data

	// Monitor orientation data
	int enable; // 1 for on, 0 for off
} assignment;

// Main of beeldverwerking thread.
int beeldverwerkingMain();

struct Line{
	Point2f p1, p2;
	float length;

	Line(): p1(0,0), p2(0,0), length(0){
	}
};

class Triangle{
public:
	Point2f a,b,c;
	float calcAngleAC() { // secretly a to c and not a to b
		Point2f vec1 = (c-a);
        return atan2(vec1.y,vec1.x)*-1;
	}
	void sortLengths() { // Sort positions so that A is attached two the longest two sides, and C to the shortest two.
		float ABl,ACl,BCl; // line lenghts
		ABl = sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
		ACl = sqrt(pow(a.x-c.x,2)+pow(a.y-c.y,2));
		BCl = sqrt(pow(b.x-c.x,2)+pow(b.y-c.y,2));
		Point2f newA, newB, newC;
		if(ABl > ACl && ABl > BCl) {
			newC = c;
			if(ACl > BCl) {
				newA = a;
				newB = b;
			} else {
				newA = b;
				newB = a;
			}
		} else if(ACl > ABl && ACl > BCl) {
			newC = b;
			if(ABl > BCl) {
				newA = a;
				newB = c;
			} else {
				newA = c;
				newB = a;
			}
		} else {
			newC = a;
			if(ACl > ABl) {
				newA = c;
				newB = b;
			} else {
				newA = b;
				newB = c;
			}
		}
		a = newA;
		b = newB;
		c = newC;

	}

	int isValidTriangle() { // returns 1 if valid
		float ABl = sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
		float ACl = sqrt(pow(a.x-c.x,2)+pow(a.y-c.y,2));
		float BCl = sqrt(pow(b.x-c.x,2)+pow(b.y-c.y,2));

		if ((ABl/ACl) >= 1.07 && (ABl/ACl) <= 1.2
			&& (ACl/BCl) >= 1.5 && (ACl/BCl) <= 2.3) {
			return 1;
		}
		return 0;
	}
/*
    Line AC, BC, AB;

    float calcAngleAB(){
        Point2f vec1 = (AB.p2 - AB.p1);

		float angle = atan2(vec1.y,vec1.x);
		//float angle = abs(asin(vec1.y/AB.length));
        return angle;
    }
    void sortLengths(){
        /// first calculate the lengths
        calcLenghts();

        /// sort lengths
        Line a = AC, b = BC, c = AB;
        if(a.length>b.length){      //a>b
            if(a.length>c.length){  //a>c
                AC = a;
            }
            else{ //a<c
                AB = a;
            }
        }
        else{ //a<b
            if(a.length>c.length){ //a>c
                AB = a;
            }
            else{ //a<c
                BC = a;
            }
        }

        if(b.length>a.length){      //b>a
            if(b.length>c.length){  //b>c
                AC = b;
            }
            else{ //a<c
                AB = b;
            }
        }
        else{ //b<a
            if(b.length>c.length){ //b>c
                AB = b;
            }
            else{ //b<c
                BC = b;
            }
        }

        if(c.length>a.length){      //c>a
            if(c.length>b.length){  //c>b
                AC = c;
            }
            else{ //c<b
                AB = c;
            }
        }
        else{ //c<a
            if(c.length>b.length){ //c>b
                AB = c;
            }
            else{ //c<b
                BC = c;
            }
        }


		return;

        /// check if points are correct
        Point2f vecAC = (AC.p2 - AC.p1);
        Point2f vecAB = (AB.p2 - AB.p1);
        Point2f vecBC = (BC.p2 - BC.p1);

        double multAC = vecAC.x - vecAC.y;
        double multAB = vecAB.x - vecAB.y;
        double multBC = vecBC.x - vecBC.y;

        Point2f XX;

        if(multAC < 0){
            XX = AC.p2;
            AC.p2 = AC.p1;
            AC.p1 = XX;
        }
        if(multAB < 0){
            XX = AB.p2;
            AB.p2 = AB.p1;
            AB.p1 = XX;
        }
        if(multBC < 0){
            XX = BC.p2;
            BC.p2 = BC.p1;
            BC.p1 = XX;
        }
    }

private:
    void calcLenghts(){
        Point2f tri_length[3];
        /// Calculate result vectors; Vr = V1 - V2
        tri_length[0] = (AC.p1-AC.p2); //AC
        tri_length[1] = (AB.p1-AB.p2); //AB
        tri_length[2] = (BC.p1-BC.p2); //BC

        /// Calculate lengths of line A, B and C
        AC.length = sqrt(tri_length[0].x*tri_length[0].x + tri_length[0].y*tri_length[0].y);
        AB.length = sqrt(tri_length[1].x*tri_length[1].x + tri_length[1].y*tri_length[1].y);
        BC.length = sqrt(tri_length[2].x*tri_length[2].x + tri_length[2].y*tri_length[2].y);
    }
*/
};

#endif // BEELDVERWERKING_H
